package com.gitee.wsl.math.random

import com.gitee.wsl.mathematics.number.compareTo
import kotlin.random.Random

fun requireGreater(num1: Number, num2: Number) {
    require(num1 <= num2) { "max must be greater than min" }
}


/**
 * Returns a random integer between [min] and [max].
 *
 * @param min the minimum value
 * @param max the maximum value
 * @return the random integer
 */
public inline fun Random.randInt(min: Int, max: Int): Int {
    if (min == max) return min
    requireGreater(min, max)
    return nextInt(max - min + 1) + min
}

/**
 * Returns a random double between [min] and [max].
 *
 * @param min the minimum value
 * @param max the maximum value
 * @return the random double
 */
public inline fun Random.randDouble(min: Double, max: Double): Double {
    requireGreater(min, max)
    return nextDouble() * (max - min) + min
}

/**
 * Returns a random float between [min] and [max].
 *
 * @param min the minimum value
 * @param max the maximum value
 * @return the random float
 */
public inline fun Random.randFloat(min: Float, max: Float): Float {
    requireGreater(min, max)
    return nextFloat() * (max - min) + min
}


/**
 * Generate random integer number within [this] range
 */
fun ClosedRange<Int>.random() = Random.nextInt(endInclusive - start) + start

/**
 * Generate random long number within [this] range
 */
fun ClosedRange<Long>.random() = Random.nextLong(endInclusive - start) + start

/**
 * Generate random float number within [this] range
 */
fun ClosedRange<Float>.random() = Random.nextFloat() * (endInclusive - start) + start

/**
 * Generate random double number within [this] range
 */
fun ClosedRange<Double>.random() = Random.nextDouble(endInclusive - start) + start
